【Unity基础】在2d的界面中获取鼠标的坐标 | 您所在的位置:网站首页 › unity 获取坐标异常 › 【Unity基础】在2d的界面中获取鼠标的坐标 |
思路:
主要用到了 RectTransformUtility里面的ScreenPointToLocalPointInRectangle函数 public static bool ScreenPointToLocalPointInRectangle (RectTransform rect, ----- 希望得到坐标结果的父对象 Vector2 screenPoint, ----- 鼠标位置 Camera cam, ----- UI摄像机 out Vector2 localPoint); ----- 相对于父对象的坐标 Vector2 localPos; RectTransformUtility.ScreenPointToLocalPointInRectangle((RectTransform)GameObject.FindGameObjectWithTag("Bottom").transform,//希望得到坐标结果的父对象 (eventData as PointerEventData).position, // 鼠标位置 (eventData as PointerEventData).pressEventCamera, //UI摄像机 out localPos); nowDragImg.transform.localPosition = localPos;//相对于父对象的坐标 |
CopyRight 2018-2019 实验室设备网 版权所有 |